﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using DataLayer;
using ExceptionLayer;

namespace CtrLayer
{
    public class ArtCtrl
    {
        private UCNArtDataContext _db = DBCon.GetDB();

        public Art FindArt(int no)
        {
            var art = _db.Arts.SingleOrDefault(a => a.Nr == no);
            return art;
        }

        public void CreateArt(string name, string desc, int artistID, string pic, double buyprice)
        {
            if (name.Length > 0 && name.Length < 41)
            {
                //NavnOK
                if (desc.Length < 81)
                {
                    //DescOK
                    if (artistID >= 0)
                    {
                        //ArtistID OK
                        if (buyprice > 0 && buyprice < 10000)
                        {
                            //buyprice OK

                            Art art = new Art();
                            art.Name = name;
                            art.Description = desc;
                            art.Picture = pic;
                            art.Buyprice = (decimal)buyprice;
                            art.Artist = 1;

                            //art.Artist = artistCtrl.findArtistByName(artistName);

                            //Hvis int er under 1000, generer et nummer automatisk
                            Random rnd = new Random();
                            int generatedN;
                            while (true)
                            {
                                generatedN = rnd.Next(1000, 9999);
                                if (FindArt(generatedN) == null)
                                {
                                    break;
                                }
                            }
                            art.Nr = generatedN;

                            _db.Arts.InsertOnSubmit(art);
                            _db.SubmitChanges();

                        }
                        else if (buyprice > 9999)
                        {
                            throw new UCNBuyPriceTooBigException();
                        }
                    }
                    //else if (artistName.Length > 40)
                    //{
                    //    throw  new UCNArtistNameTooLongException();
                    //}
                    //else if (artistName.Length < 1)
                    //{
                    //    throw  new UCNArtistNameTooShortException();
                    //}
                }
                else
                {
                    throw new UCNDescTooLongException();
                }
            }
            else if (name.Length < 1)
            {
                throw new UCNNameTooShortException();
            }
            else if (name.Length > 40)
            {
                throw new UCNNameTooLongException();
            }

        }

        public Art SearchArt(int artNo)
        {
            Art art = null;
            try
            {
                art = _db.Arts.SingleOrDefault(a => a.Nr == artNo);
            }
            catch
            {
                
            }
            return art;
        }

        public void UpdateArt(int no, int newNo, string name, string desc, int artistID, string pic, double buyprice)
        {
            Art art = FindArt(no);
            if (art != null)
            {
                if (name.Length > 0 && name.Length < 41)
                {
                    //NavnOK
                    if (desc.Length < 81)
                    {
                        //DescOK
                        if (artistID >= 0)
                        {
                            //ArtistName OK
                            if (buyprice > 0 && buyprice < 10000)
                            {
                                //buyprice OK

                                art.Name = name;
                                art.Description = desc;
                                art.Picture = pic;
                                art.Buyprice = (decimal)buyprice;
                                art.Artist = artistID;

                                //art.Artist = artistCtrl.findArtistByName(artistName);

                                //Hvis int er under 1000, generer et nummer automatisk
                                if (newNo < 1000)
                                {
                                    Random rnd = new Random();
                                    int generatedN;
                                    while (true)
                                    {
                                        generatedN = rnd.Next(1000, 9999);
                                        if (FindArt(generatedN) == null)
                                        {
                                            break;
                                        }
                                    }
                                    art.Nr = generatedN;
                                }
                                else if (newNo >= 1000 && newNo < 10000)
                                {
                                    if (FindArt(newNo) == null)
                                    {
                                        art.Nr = newNo;
                                    }
                                    else
                                    {
                                        throw new UCNNoAlreadyExsistException();
                                    }
                                }
                                else if (newNo > 9999)
                                {
                                    throw new UCNNoTooLongException();
                                }
                                else if (newNo < 1000)
                                {
                                    throw new UCNNoTooShortException();
                                }

                                FindArt(no).Name = art.Name;
                                FindArt(no).Description = art.Description;
                                FindArt(no).Artist = art.Artist;
                                FindArt(no).Buyprice = art.Buyprice;
                                FindArt(no).Picture = art.Picture;
                                FindArt(no).Nr = art.Nr;

                                //_db.Arts.InsertOnSubmit(art);
                                _db.SubmitChanges();

                            }
                            else if (buyprice > 9999)
                            {
                                throw new UCNBuyPriceTooBigException();
                            }
                        }
                        //else if (artistName.Length > 40)
                        //{
                        //    throw new UCNArtistNameTooLongException();
                        //}
                        //else if (artistName.Length < 1)
                        //{
                        //    throw new UCNArtistNameTooShortException();
                        //}
                    }
                    else
                    {
                        throw new UCNDescTooLongException();
                    }
                }
                else if (name.Length < 1)
                {
                    throw new UCNNameTooShortException();
                }
                else if (name.Length > 40)
                {
                    throw new UCNNameTooLongException();
                }
            }
            else
            {
                throw new UCNArtNotFoundException();
            }
        }

        public void DeleteArt(int artNo)
        {
            Art art = _db.Arts.SingleOrDefault(a => a.Nr == artNo);
            if (art != null)
            {
                _db.Arts.DeleteOnSubmit(art);
            }
            else
            {
                throw new UCNEmptyException();
            }
        }

    }
}
